package io.github.talelin.latticy.repository;

import io.github.talelin.latticy.entity.Record;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface RecordRepository extends JpaRepository<Record, Long> {

    Page<Record> findByUserIdOrderByCreateTimeDesc(Long userId, Pageable pageable);

    Page<Record> findByExamineUserIdAndStatusInOrderByCreateTimeDesc(Long examineUserId, List<Integer> status, Pageable pageable);

    Page<Record> findByRootUserIdOrderByCreateTimeDesc(Long rootUserId, Pageable pageable);

    Record findFirstById(Long id);

    Record findByTypeAndTypeId(int type, Long typeId);

    @Query(value = "select count(*) as count \n" +
            "from record \n" +
            "where \n" +
            "examine_user_id = :examineUserId \n" +
            "and \n" +
            "status = 1 \n" +
            "and \n" +
            "delete_time is null", nativeQuery = true)
    Integer findExamineUserDealNum(Long examineUserId);

    @Query(value = "select count(*) as count \n" +
            "from record \n" +
            "where \n" +
            "user_id = :userId \n" +
            "and \n" +
            "feedback = 1\n" +
            "and \n" +
            "delete_time is null", nativeQuery = true)
    Integer findUserFeedbackNum(Long userId);
}
